package org.example.chat_room.api;

import jakarta.annotation.Resource;
import jakarta.annotation.Resources;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.example.chat_room.Utils.MD5Util;
import org.example.chat_room.Utils.UUIDUtil;
import org.example.chat_room.model.User;
import org.example.chat_room.model.UserMapper;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.awt.*;
import java.util.UUID;


@RestController
public class UserAPI {
    @Resource
    UserMapper userMapper;

    @PostMapping("/login")
    @ResponseBody
    public Object login(String username, String password, HttpServletRequest request){
        User user = userMapper.selectUserByName(username);
        if (user == null){
            System.out.println("登录失败，请输入正确的用户名或密码. user="+user);
            return new User();
        }
        //  对密码做校验
        String encryptPassword = MD5Util.md5Salt(password, user.getSalt());
        System.out.println(encryptPassword);
        //  用密文和数据库中存的用户密码进行比较
        if (!encryptPassword.equalsIgnoreCase(user.getPassword())) {
            return new User();
        }
        HttpSession session = request.getSession(true);
        session.setAttribute("user", user);
        user.setPassword("");
        System.out.println("....");
        return user;
    }

    @PostMapping("/register")
    @ResponseBody
    public Object register(String username,String password,String passwordRepeat){
        // 校验密码与重复密码是否相同
        if (!password.equals(passwordRepeat)) {
            return new User();
        }

        User user = null;
        try {
            user = new User();
            user.setUsername(username);
            // 1. 生成盐
            String salt = UUIDUtil.UUID_32();
            // 2. 生成密码的密文
            String encryptPassword = MD5Util.md5Salt(password,salt);
            // 3. 设置密码和盐
            user.setPassword(encryptPassword);
            user.setSalt(salt);
            user.setAvatarPath("/images/user1.png");

            int ret = userMapper.insertUser(user);

            System.out.println("注册 ret："+ret);
            user.setPassword("");
        }catch (DuplicateKeyException e){
            user =  new User();
            System.out.println("注册失败！username="+username+"已经存在");
        }
        return user;
    }

    @GetMapping("/userInfo")
    @ResponseBody
    public Object getUserInfo(HttpServletRequest request){
//        1.从请求中获取会话
//        2.从会话中获取之前保存的用户对象
//        肯定是false啊，获取之前的对象，要是没有的话那就直接返回
        HttpSession session = request.getSession(false);
        if (session == null){
//            用户未登录
            System.out.println("[getUserInfor] session is null");
            return new User();
        }
        User user = (User) session.getAttribute("user");
        if (user == null){
            System.out.println("[getUserInfor] user is null");
            return new User();
        }
        user.setPassword("");
        return user;
    }

    @GetMapping("/getAvatar")
    public User getAvatar(HttpServletRequest request) {
        HttpSession session = request.getSession(false);
        if (session == null){
//            用户未登录
            System.out.println("[getUserInfor] session is null");
            return new User();
        }
        User user = (User) session.getAttribute("user");
        if (user == null){
            System.out.println("[getUserInfor] user is null");
            return new User();
        }
        return userMapper.selectUserByName(user.getUsername());
    }


}
